import random


class MyRandom:
    _chinese_list = []
    _english_list = []

    def get_chinese_list(self):
        """ 获取所有汉字字符列表 """
        for i in range(176, 216):
            s = bytes([i])
            for x in range(161, 255):
                s += bytes([x])
                try:
                    c = s.decode("gb2312")
                except:
                    break
                self._chinese_list.append(c)
                s = bytes([i])

    def get_english_list(self):
        """ 获取所有英文字母列表，前26个是大写，后面是小写 """
        self._english_list = list(set([chr(i) for i in range(97, 123)]) | set([chr(i) for i in range(65, 91)]))
        self._english_list.sort()

    def random_int(self, start=1, end=100):
        """ 获取start到end之间的整形数 """
        return random.randint(start, end)

    def random_zh(self, len=None):
        """ 获取指定个数的随机中文字符，len默认随机1-5 """
        if not len:
            len = self.random_int(end=5)
        # random.choice(self._chinese_list)
        if not self._chinese_list or len(self._chinese_list) == 0:
            self.get_chinese_list()
        zh_list = random.choices(self._chinese_list, k=len)
        return ''.join(zh_list)

    def random_en(self, len=None, word_type=0):
        """
        获取随机英文
        @param:
            len: 需要的随机英文字符个数，默认随机1-5
            word_type: 0-大小写混合，1-大写，2-小写
        """
        if not len:
            len = self.random_int(end=5)
        if self._english_list is not None or len(self._english_list) == 0:
            self.get_english_list()
        words = self._english_list if word_type == 0 else (self._english_list[0:26] if word_type == 1 else self._english_list[26:52])
        en_list = random.choices(words, k=len)
        return ''.join(en_list)


if __name__ == '__main__':
    myrandom = MyRandom()
    # myrandom.get_english_list()
    print(myrandom.random_en(len=20, word_type=2))
